package GUI;

import MachineLogic.*;
import PaymentModule.*;
import java.awt.Color;
import java.util.Date;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class tvmGUI extends javax.swing.JFrame {

    /**
     * Creates new form tvmGUI
     */
    public tvmGUI() {
        this.ListContent = new ArrayList<>();
        //Date time = new Date();
        initComponents();       
        // Initialize labels to be invisible
        
        // Initialize error labels to be empty/single space
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setup11 = new GUI.Setup1();
        setup21 = new GUI.Setup2();
        welcomeDA1 = new GUI.WelcomeDA();
        cartDA1 = new GUI.CartDA();
        payDA1 = new GUI.PayDA();
        cashDA1 = new GUI.CashDA();
        outOfOrder1 = new GUI.OutOfOrder();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(820, 630));
        getContentPane().setLayout(new java.awt.CardLayout());
        getContentPane().add(setup11, "card2");
        getContentPane().add(setup21, "card3");
        getContentPane().add(welcomeDA1, "card4");
        getContentPane().add(cartDA1, "card5");
        getContentPane().add(payDA1, "card6");
        getContentPane().add(cashDA1, "card7");
        getContentPane().add(outOfOrder1, "card8");

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Variables
    
    /**
     * Checks on a string if it is an integer.
     * @param s String input to be checked for integers.
     * @return Returns the integer if any, or -1 if no integer found.
     */
    public int isInteger(String s)
    {
        int temp;
        try {
            temp = Integer.parseInt(s);        // Try make s to int
        } catch (NumberFormatException e) {    //catch exception - return error
            return -1;
        }
        if (temp < 0) {                     // invert negatives
            temp *= -1;
        }
        return temp; 
    }   
    
    /**
     * Formate a string to be 15 characters long, by inserting spaces.
     * @param item String to be formated.
     * @return The formated string.
     */
    public String fitToListCart(String item) {
        while (item.length() < 15) {
            item += " ";
        }
        return item;
    }
    
    /**
     * Prints the tickets in the shopping cart and updates statistics. 
     * If error arises during printing a dialog window is opened.
     * @param window The JPanel from which the call is made. Is hidden if 
     * a printer error is encountered.
     */
    public void printTickets(JPanel window) {
        //Print all tickets in the basket
        for (int index=0;index<(SB.getCart().size());index++) {
            if (SB.printTicket(index,language,hardID)== false){      //if printing failed
                // Show dialog box with error message
                JOptionPane.showMessageDialog(this, 
                    "\nFejl"
                   + "\nDer opstod en fejl ved printning af en billet."
                   + "\nKontakt tekniker på tlf. xx xx xx xx og"
                   + "\nhusk dine returpenge."
                   + "\nVi beklager ulegligheden."
                   + "\n"
                   + "\nBlueJ Trakfikselskab","Teknisk fejl",
                JOptionPane.ERROR_MESSAGE);
                // Set info label and out-of-order variable
                //LabOutOfOrderInfo.setText("Maskin ID: "+hardID+"  Zone:"+startZone);
                outOfOrder = true;
                // Go to out-of-order window
                window.setVisible(false);
                //OutOfOrder.setVisible(true);
                break;
            } else {
                // Send the ticket to the statistics module
                ST.LogSale(SB.getCart().get(index));
            }      
        }
    }
    
    /**
     * Set the visibility of current JPanel to false, and true on the JPanel
     * that needs displaying next. 
     * @param currentWindow The Jpanel from which the method is called, and needs to be hidden.
     * @param nextWindow The JPanel that needs to be displayed next.
     */
    public void switchWindow(JPanel currentWindow, JPanel nextWindow) {
        currentWindow.setVisible(false);
        nextWindow.setVisible(true);
    }
    
    //Variables
    Date time = new Date();
    int hardID;
    int startZone;
    CreatedTickets CT = new CreatedTickets();
    Statistics ST = new Statistics();       
    ShoppingBasket SB = new ShoppingBasket(CT, ST);
    Cash CH = null;
    CreditCard CC = null;
    paySMS SMS = null;
    ArrayList<String> ListContent;
    ArrayList<String> CartContent = new ArrayList<String>();
    int amountZones = 0;
    int amountTickets = 0;
    int typePricePZ = 0;
    int language = 1;   //1 danish; 2 english
    boolean outOfOrder = false;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(tvmGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(tvmGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(tvmGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(tvmGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new tvmGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private GUI.CartDA cartDA1;
    private GUI.CashDA cashDA1;
    private GUI.OutOfOrder outOfOrder1;
    private GUI.PayDA payDA1;
    private GUI.Setup1 setup11;
    private GUI.Setup2 setup21;
    private GUI.WelcomeDA welcomeDA1;
    // End of variables declaration//GEN-END:variables
}
